Self-Learned Algorithms - 07
This is my learning note about algorithms.
Reference
- 《小浩算法》) - 排序系列
905.按奇偶排序数组
https://leetcode-cn.com/problems/sort-array-by-parity/
题解
- 奇偶数的话维护双指针就可以了
解法
- 按照模2的结果排序
1 | A.sort(key = lambda x: x % 2) |
- 奇数数组和偶数数组拼接
1 | ans1 = [] |
- 双指针同向遍历
1 | i = 0 |
- 双指针双向遍历
1 | i, j = 0, len(A)-1 |